package com.senmiao.ssm.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senmiao.ssm.entity.Employee;
import com.senmiao.ssm.entity.vo.EmployeePage;
import com.senmiao.ssm.mapper.EmployeeMapper;
import com.senmiao.ssm.service.EmployeeService;
import com.senmiao.ssm.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 通过id查员工信息
     * @param id
     * @return
     */
    @Override
    public Result getById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        if (employee != null){
            return Result.ok().message("获取成功").data("data",employee);
        }
        return Result.error().message("id为"+id+"的员工不存在");
    }

    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    @Override
    public Result add(Employee employee) {
        int insert = employeeMapper.insert(employee);
        if (insert > 0){
            return Result.ok().message("新增成功");
        }
        return Result.error().message("新增失败，服务器异常");
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @Override
    public Result updateEmp(Employee employee) {
        int update = employeeMapper.updateById(employee);
        if (update > 0){
            return Result.ok().message("更新员工信息成功");
        }
        return Result.error().message("更新员工信息失败，服务器异常");
    }

    /**
     * 分页获取员工列表
     * @param pageNum 页码
     * @return 分页信息以及员工数据
     */
    @Override
    public Result getEmployees(Integer pageNum) {
        EmployeePage employeePage = new EmployeePage();

        int pageSize = 10;//分页条数
        Page<Employee> mybatisPlusPage = employeeMapper.selectPage(new Page<Employee>(pageNum, pageSize), null);
        List<Employee> records = mybatisPlusPage.getRecords();
        long total = mybatisPlusPage.getTotal();
        long totalPage = mybatisPlusPage.getPages();

        employeePage.setEmployees(records);
        employeePage.setTotal(total);
        employeePage.setTotalPage(totalPage);
        employeePage.setPage(pageNum.longValue());

        return Result.ok().message("获取成功").data("data",employeePage);
    }

    /**
     * 根据id删除员工信息
     * @param ids id数组
     * @return
     */
    @Override
    public Result deleteEmp(List<Integer> ids) {
        int i = employeeMapper.deleteBatchIds(ids);
        if (i > 0){
            return Result.ok().message("删除员工成功");
        }
        return Result.error().message("删除员工失败，服务器异常");
    }
}
